local UIBattleManyTeams     = UIBase.New(PanelResNames.UIBattleManyTeams)

local m_team_list           = {}

function UIBattleManyTeams.Init()
    UIBase.Init(UIBattleManyTeams)

    -- FormationManager.req_FormationList()
end

function UIBattleManyTeams:Awake()
    
end

function UIBattleManyTeams:OnCreate()
    self:RegisterOnClick(self:GetChild("bgclose"), self.OnClick_close)
    self:RegisterOnClick(self:GetChild("btnBack"), self.OnClick_close)

    self.list               = self:GetChild("list")
    self.list.itemRenderer  = self.TeamItemRender

    self:RefreshList()

    -- self:RegisterTouchBegin(self.Slider_Voice:GetChild("grip"),self.onTouchStartVoice)
    -- self:RegisterTouchEnd(self.Slider_Voice,self.onTouchEndVoice)
    self:RegisterEvent(Config.EventType.RefreshFormationList, self.RefreshList)
    self:RegisterEvent(Config.EventType.FormationSetFinish, function()
        self:RefreshList()
        UISysTips.AddMsg(getLanguage("UIBattleManyTeams1011"))
        closeUI(PanelNames.UIBattleTeam)
    end)

    -- FormationManager.req_FormationList()
end

function UIBattleManyTeams.TeamItemRender(index, obj)
    local ctr_mask = obj:GetController("mask")
    local ctr_btnlist = obj:GetController("ButtonList")
    local heros = obj:GetChild("list_herohead")
    local tips = obj:GetChild("tips")
    local btn_rename = obj:GetChild("ButtonTips1")
    local btn_top = obj:GetChild("ButtonTips2")
    local btn_remove = obj:GetChild("ButtonTips3")
    local btn_bg = obj:GetChild("btn_bg")
    local btn_supskill = obj:GetChild("btnSupSkill")
    local img_supskill = btn_supskill:GetChild("icon"):GetChild("icon")
    

    btn_rename:GetChild("Tips").text = getLanguage("UIBattleManyTeams1006")
    btn_top:GetChild("Tips").text = getLanguage("UIBattleManyTeams1007")
    btn_remove:GetChild("Tips").text = getLanguage("UIBattleManyTeams1008")

    local editor = false
    
    if m_team_list[index + 1] then
        tips.text = m_team_list[index + 1].name
        ctr_mask.selectedIndex = 0
        editor = true

        local data = G.dataTable["t_skillshow"][tostring(m_team_list[index + 1].skill)];
        if data then
            img_supskill.url =  "ui://SkillAtlas/" .. data.f_SkillIcon
        else
            img_supskill.url =  ""
        end
        
    else
        tips.text = getLanguage("UIBattleManyTeams1001") .. (index + 1)
        img_supskill.url =  ""
        if (index + 1) - table.getn(m_team_list) == 1 then
            ctr_mask.selectedIndex = 0
            editor = true
        else
            ctr_mask.selectedIndex = 1
            editor = false
        end
    end

    ctr_btnlist.selectedIndex = editor and 1 or 0

    btn_rename.onClick:Clear()
    btn_rename.onClick:Add(function()
        if m_team_list[index + 1] then
            showUI(PanelResNames.UITeamsRename, m_team_list[index + 1])
        else
            UISysTips.AddMsg(getLanguage("UIBattleManyTeams1010"))
        end
    end)
    btn_top.onClick:Clear()
    btn_top.onClick:Add(function()
        if m_team_list[index + 1] then
            FormationManager.req_FormationSort(index + 1)
        else
            UISysTips.AddMsg(getLanguage("UIBattleManyTeams1010"))
        end
    end)
    btn_remove.onClick:Clear()
    btn_remove.onClick:Add(function()
        if m_team_list[index + 1] then
            FormationManager.req_FormationRemove(index + 1)
        else
            UISysTips.AddMsg(getLanguage("UIBattleManyTeams1010"))
        end
    end)
    
    heros.itemRenderer = function(child, it)
        local ctr_state = it:GetController("state")
        local Btn_Hero = it:GetChild("Btn_Hero")
        local ctr_hadBattle = Btn_Hero:GetController("ctrlHadBattle")
        local ctr_heroPos = it:GetController("HeroPos")
        if m_team_list[index + 1] then
            local pid = m_team_list[index + 1].team[child + 1]
            ctr_state.selectedIndex = pid == "0" and 0 or 1
            if pid ~= "0" then
                local lua = CtrlManager.GetCtrl(PanelNames.HeadRectItem)
                local h = HeroManager.getHeroDatabyPartnerId(pid)
                lua.initData(Btn_Hero, h)

                ctr_hadBattle.selectedIndex = 0

                if ctr_heroPos then
                    ctr_heroPos.selectedIndex = child + 1
                end
            else
                ctr_heroPos.selectedIndex = 0
            end
        else
            ctr_state.selectedIndex = 0
            ctr_heroPos.selectedIndex = 0
        end
    end
    heros.numItems = 6
    local ms
    local holding = false

    btn_bg.onClick:Add(function()
        if editor then
            FormationManager.SetFocus(index + 1)
            
            -- showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.FormationEditor, nil, function(team)
            --     local sup = SkillManager.getSupSkillIdByFormationType(EnumConst.FormationType.FormationEditor) or 0
            --     FormationManager.req_SetFormation(index + 1, team[1])
            -- end)

            showUI(PanelResNames.UIBattleTeamEditor, EnumConst.FightTypeConst.FormationEditor, nil, function(team)
                FormationManager.req_SetFormation(index + 1, team[1])
                closeUI(PanelResNames.UIBattleTeamEditor)
            end)
        end
    end)
end

function UIBattleManyTeams:RefreshList()
    m_team_list = FormationManager.GetAllFormation()
    self.list.numItems = logicMgr.FormationManager.GetMax()
end

function UIBattleManyTeams.MoveToIndex(index)
    UIBattleManyTeams.list:ScrollToView(index)
end

function UIBattleManyTeams:OnClick_close()
    closeUI(PanelResNames.UIBattleManyTeams)
end

return UIBattleManyTeams